package com.nervenets.template.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.nervenets.general.entity.WsMessage;
import com.nervenets.general.jwt.aspect.JwtSecurity;
import com.nervenets.general.service.WebSocketService;
import com.nervenets.general.utils.NumberUtil;
import com.nervenets.general.utils.StringUtils;
import com.nervenets.general.web.BaseController;
import com.nervenets.template.enumeration.AttachmentType;
import com.nervenets.template.hibernate.domain.common.*;
import com.nervenets.template.hibernate.domain.user.Manager;
import com.nervenets.template.service.AddressService;
import com.nervenets.template.service.FeedbackService;
import com.nervenets.template.service.GlobalRuleService;
import com.nervenets.template.service.UserService;
import com.nervenets.template.service.subpub.PubMessageHandler;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Slf4j
@Controller
@RequestMapping("/api/test")
public class TestController extends BaseController {
    @Reference(version = "1.0.0", check = false)
    private WebSocketService webSocketService;
    @Resource
    private CacheManager cacheManager;
    @Resource
    private PubMessageHandler pubMessageHandler;
    @Resource
    private GlobalRuleService globalRuleService;
    @Resource
    private FeedbackService feedbackService;

    @ApiOperation("是否存活")
    @GetMapping("/alive")
    @JwtSecurity(required = false)
    @ResponseBody
    public String alive() {
        return "OK";
    }

    @ApiOperation("微信跳转")
    @PostMapping("/socketMessageSend")
    @JwtSecurity(required = false)
    public ResponseEntity socketMessageSend(String topicId, WsMessage message) {
        webSocketService.convertAndSend(topicId, message);
        return successMessage();
    }

    @ApiOperation("缓存测试")
    @PostMapping("/cacheTest")
    @JwtSecurity(required = false)
    public ResponseEntity cacheTest() {
        return successMessage(feedbackService.findById(8451717068641931264L).orElse(null));
    }

    @ApiOperation("Redis消息队列测试")
    @PostMapping("/message")
    @JwtSecurity(required = false)
    public ResponseEntity redisMessage() {
        return successMessage(pubMessageHandler.pub(Manager.builder()
                .id((long) NumberUtil.randomNumber(100, 99999))
                .name(StringUtils.randomString(10))
                .build()));
    }
}
